﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Windows.Forms;
//using System.Threading;
//using CefSharp.WinForms;
//using CefSharp;
//using Citi.SecurityHelper;
//using Citi.SecurityWebBrowser;

//namespace CefSharp.WinForms
//{
//    public static class SingleSignOnCef
//    {
//        private static string originalUrl;
//        private static string Soeid;
//        private static Citi.SecurityHelper.LoginType LoginType;
//        //private static bool urlChanged;
//        private static bool Logged { get; set; }
//        //private static WebView browser;

//        public static bool IsLogged(this WebView webView)
//        {
//            return Logged;
//        }

//        static void webView_LoadCompleted(object sender, LoadCompletedEventArgs url)
//        {
//            LogUser((WebView)sender);
//        }

//        public static void LogSingleSignOn(this WebView webView, string soeid, Citi.SecurityHelper.LoginType loginType, string url = null)
//        {
//            originalUrl = originalUrl ?? url;
//            Soeid = soeid;
//            LoginType = loginType;
//            Logged = false;

//            if (!string.IsNullOrWhiteSpace(url))
//                webView.Load(url);

//            webView.LoadCompleted += webView_LoadCompleted;

//            while (webView.IsLoading)
//            {
//                Application.DoEvents();
//                Thread.Sleep(100);
//            }

//            //LogUser(webView);
//        }

//        private static string LogScript(string user, string password)
//        {
//            var javascript = string.Format(
//                @"var user = document.getElementsByName('USER')[0];
//                user.value = '{0}'
//
//                var password = document.getElementsByName('PASSWORD')[0];
//                password.value = '{1}';
//                
//                try {{   
//                    submitForm();
//                }}   
//                catch (err) {{
//                    fn_login();
//                }}", user, password);

//            return javascript;
//        }

//        private static void LogUser(WebView webView)
//        {
//            var password = CitiEncryption.GetDecryptedPassword(Soeid, LoginType, SecurityConfigurations.RobotUserName, SecurityConfigurations.RobotPassword);

//            var userLength = (int)webView.EvaluateScript("document.getElementsByName('USER').length");

//            if (userLength > 0)
//            {
//                webView.ExecuteScriptAsync(LogScript(Soeid, password));
//                webView.LoadCompleted -= webView_LoadCompleted;
//            }
//            //webView.LoadCompleted += logged_LoadCompleted;
//        }

//        static void logged_LoadCompleted(object sender, LoadCompletedEventArgs url)
//        {
//            var webView = (WebView)sender;

//            while (webView.IsLoading)
//            {
//                Application.DoEvents();
//                Thread.Sleep(100);
//            }

//            Logged = true;
//        }
//    }
//}